/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;

/**
 *
 * @author Phong
 */
public class UserListDAO {
      public static boolean Them(UserList n) {
        try {
             Connection conn = DBConnect.Connect();
            Statement statement = conn.createStatement();
            String sql = String.format ("INSERT INTO userlist(id_nd, username, pass,idgroup,ngaytao,kichhoat) VALUES('%d','%s','%s','%s','%s','%d')"
                    ,n.Id,n.UserName,n.Pass,n.IdGroup,n.NgayTao,n.KichHoat);
            statement.executeUpdate(sql);
            conn.close();
            return true;
           // JOptionPane.showMessageDialog(null, "Thêm thành công!!!", "Thông báo", 1);

        } catch (Exception ex) {
           // JOptionPane.showMessageDialog(null, "Thêm Không thành công!!!", "Thông báo", 1);
        }
        return false;
    }
      public static boolean Xoa(String id) {
        try {
            Connection conn = DBConnect.Connect();
            String strSQL = "delete from userlist where id_nd = ?";
            PreparedStatement pst = conn.prepareStatement(strSQL);
            pst.setString(1, id);
            pst.executeUpdate();
            //JOptionPane.showMessageDialog(null, "Xóa thành công!!!", "Thông báo", 1);
            pst.close();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
           // JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
        }
        return false;
    }
          public static boolean Update(UserList NV)
    {
     try
        {
            Connection con = DBConnect.Connect();
            String sql = "Update userlist set username = ?, pass = ?"
                    + " Where id_nd = ?";
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, NV.UserName);
            ps.setString(2, NV.Pass);
            ps.setInt(3, NV.Id);
       
            int n = ps.executeUpdate();
            con.close();
            
            if (n == 1)
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
      
      
      
}
